﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Gammar.Price.Web");

Gammar.Price.Web.PagerControl = function(element) {
    Gammar.Price.Web.PagerControl.initializeBase(this, [element]);
    
    this._pageCount = 10;
}

Gammar.Price.Web.PagerControl.prototype = {
    initialize: function() {
        Gammar.Price.Web.PagerControl.callBaseMethod(this, 'initialize');
        
        // Add custom initialization here
    },
    
    updated: function() {
        var div = this.get_element();
        if (this.get_DataCount() === 0) {
            div.style.display = "none";
            return;
        }
        var dataCountSpan = document.createElement("SPAN");
        div.appendChild(dataCountSpan);
        var _pageTotalCount = Math.ceil(this.get_DataCount() / this.get_PageSize());
        dataCountSpan.innerHTML = String.format("共 {0} 条记录 {1}/{2} 页", this.get_DataCount(), this.get_PageIndex() + 1, _pageTotalCount);
        
        if (_pageTotalCount < 2) return;
        var pageIndex = this.get_PageIndex();
        var pageSize = this.get_PageSize();
        var pageCount = this.get_PageCount();
        var start = pageIndex - 3;
        if (start < 0) start = 0;
        var lastPageIndex = Math.ceil(this.get_DataCount() / pageSize);
        if (start + pageCount > lastPageIndex) start = lastPageIndex - pageCount;
        if (start < 0) start = 0;
        var end = Math.min(pageCount + start, lastPageIndex);
        
        var first = document.createElement("A");
        this._buildAnchor(first, "第一页");        
        $addHandler(first, "click", Function.createDelegate(this, Function.createCallback(this._raisePageClick, 0)));            
        div.appendChild(first);
        
        for(var i = start;i < end;i++) {
            if (this.get_PageIndex() === i) {
                var f = document.createElement("B");
                f.innerHTML = i + 1;
                div.appendChild(f);
            }
            else {
                var a = document.createElement("A");
                this._buildAnchor(a, i + 1);
                $addHandler(a, "click", Function.createDelegate(this, Function.createCallback(this._raisePageClick, i)));
                div.appendChild(a);
            }
        }
        
        var last = document.createElement("A");
        this._buildAnchor(last, "最后一页");
        $addHandler(last, "click", Function.createDelegate(this, Function.createCallback(this._raisePageClick, lastPageIndex - 1)));
        div.appendChild(last);
        
        // direction navigator
        var nav = document.createElement("INPUT");
        nav.type = "text";
        nav.size = 2;
        nav.maxLength = 4;
        $addHandler(nav, "keypress", Function.createDelegate(this, this._keyPress));
        div.appendChild(nav);
        
        var navb = document.createElement("A");
        this._buildAnchor(navb, "<b>>></b>");
        navb.title = "跳转到";
        $addHandler(navb, "click", Function.createDelegate(this, Function.createCallback(this._raisePageClick, nav)));
        div.appendChild(navb);
    },
    
    _keyPress: function(eventObject) {
        if (eventObject.charCode === 13) {
            eventObject.preventDefault();
            eventObject.stopPropagation();            
            this._raisePageClick(eventObject, eventObject.target);
        }
    },
    
    _buildAnchor: function(link, text) {
        link.innerHTML = text;
        link.href = "javascript:void(0);";
    },
    
    _raisePageClick: function(eventObject, value) {
        var handler = this.get_events().getHandler("pageClick");
        if (handler) {
            var pageIndex = 0;
            if (typeof(value) === 'number') {
                pageIndex = value;
            }
            else if (typeof(value) === 'object') {
                if (value.value.length === 0) {
                    alert("请输入您要跳转的页码！");
                    return;
                }
                pageIndex = parseInt(value.value, 10) - 1;
                var pageSize = this.get_PageSize();
                var lastPageIndex = Math.ceil(this.get_DataCount() / pageSize);
                if (pageIndex > lastPageIndex - 1) {
                    alert(String.format("您输入的页码太大！最后一页页码为 {0}", lastPageIndex));
                    value.select();
                    return;
                }
            }
            else {
                alert("导航错误！参数 value 未知类型");
                return;
            }
            if (pageIndex < 0) pageIndex = 0;
            handler(eventObject, {pageIndex: pageIndex});
        }
    },
    
    add_pageClick: function(handler) {
        /// <summary></summary>
        /// <param name="handler" type="Function" />
        var e = Function._validateParams(arguments, [
            {name: "handler", type: Function}
        ]);
        if (e) throw e;        
        this.get_events().addHandler("pageClick", handler);
    },
    
    remove_pageClick: function(handler) {
        /// <summary></summary>
        /// <param name="handler" type="Function" />
        this.get_events().removeHandler("pageClick", handler);
    },
    
    set_DataCount: function(value) {
        /// <summary></summary>
        /// <param name="value" type="Number" integer="true" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: Number, integer: true}
        ]);
        if (e) throw e;        
        this._dataCount = value;
    },
    
    get_DataCount: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._dataCount;
    },
    
    set_PageIndex: function(value) {
        /// <summary></summary>
        /// <param name="value" type="Number" integer="true" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: Number, integer: true}
        ]);
        if (e) throw e;        
        this._pageIndex = value;
    },
    
    get_PageIndex: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._pageIndex;
    },
    
    set_PageCount: function(value) {
        /// <summary></summary>
        /// <param name="value" type="Number" integer="true" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: Number, integer: true}
        ]);
        if (e) throw e;        
        this._pageCount = value;
    },
    
    get_PageCount: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._pageCount;
    },
    
    set_PageSize: function(value) {
        /// <summary></summary>
        /// <param name="value" type="Number" integer="true" />
        var e = Function._validateParams(arguments, [
            {name: "value", type: Number, integer: true}
        ]);
        if (e) throw e;        
        this._pageSize = value;
    },
    
    get_PageSize: function() {
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._pageSize;
    },
    
    dispose: function() {        
        //Add custom dispose actions here

        Gammar.Price.Web.PagerControl.callBaseMethod(this, 'dispose');
    }
}
Gammar.Price.Web.PagerControl.registerClass('Gammar.Price.Web.PagerControl', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
